package com.thatvn.diemtinvietnam;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.thatvn.diemtinvietnam.Config;

public class Utils {
	public static String[] allMenuItem = new String[]{"Tin nóng","Xã Hội", "Thế Giới", "Kinh Tế", "Văn Hóa", "Thể Thao", "Pháp Luật", "Khoa Học", "Công Nghệ", "Ô Tô - Xe Máy", "Giáo Dục", "Sức Khỏe", "Giải Trí","","","",""};
    public static void CopyStream(InputStream is, OutputStream os)
    {
    	/////////////// Neu bai viet cua minh co ich cho ban thi ung ho app cua minh voi nhe
		// https://play.google.com/store/apps/developer?id=GameAppAndroiFree
    	
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }
    public static String getCatName(String catid){
    	int index=Integer.parseInt(catid);
    	if (index<0 || index>=allMenuItem.length) return "";
    	String temp=allMenuItem[index];
    	return temp;
    }
    public static String getDomainName(int index){
    	if (index==0) return "vnexpress";
    	if (index==1) return "dantri";
    	if (index==2) return "zing";
    	if (index==3) return "24h";
    	if (index==4) return "kenh14";
    	if (index==5) return "doisongphapluat";
    	if (index==6) return "thethao247";
    	if (index==7) return "ngoisao";
    	if (index==8) return "vietnamnet";
    	if (index==9) return "laodong";
    	if (index==10) return "afamily";
    	if (index==11) return "tuoitre";
    	if (index==12) return "thanhnien";
    	return "vnexpress";
    }
    public static String smoothDomain(String domain){
    	return domain.replace("http://www.", "").replace("http://", "");
    }
    public static String getDomainImage(String domain) {
        if (domain.contains("24h")) return Config.domain + "/Images/Icon/24h.jpg";
        if (domain.contains("cafef")) return Config.domain + "/Images/Icon/cafef.jpg";
        if (domain.contains("dantri")) return Config.domain + "/Images/Icon/dantri.jpg";
        if (domain.contains("kenh14")) return Config.domain + "/Images/Icon/kenh14.jpg";
        if (domain.contains("laodong")) return Config.domain + "/Images/Icon/laodong.jpg";
        if (domain.contains("ngoisao")) return Config.domain + "/Images/Icon/ngoisao.jpg";
        if (domain.contains("thethaovanhoa")) return Config.domain + "/Images/Icon/thethaovanhoa.jpg";
        if (domain.contains("vietnamnet")) return Config.domain + "/Images/Icon/vietnamnet.jpg";
        if (domain.contains("vnexpress")) return Config.domain + "/Images/Icon/vnexpress.jpg";
        if (domain.contains("vtc")) return Config.domain + "/Images/Icon/vtc.jpg";  
        if (domain.contains("thethao247")) return Config.domain + "/Images/Icon/thethao247.jpg"; 
        if (domain.contains("thanhnien")) return Config.domain + "/Images/Icon/thanhnien.jpg";  
        if (domain.contains("vietbao")) return Config.domain + "/Images/Icon/vietbao.jpg"; 
        if (domain.contains("hanoimoi")) return Config.domain + "/Images/Icon/hanoimoi.jpg"; 
        if (domain.contains("doisongphapluat")) return Config.domain + "/Images/Icon/doisongphapluat.jpg"; 
        if (domain.contains("danviet")) return Config.domain + "/Images/Icon/danviet.jpg";
        if (domain.contains("zing")) return Config.domain + "/Images/Icon/zing.jpg";
        if (domain.contains("afamily")) return Config.domain + "/Images/Icon/afamily.jpg";
        if (domain.contains("tuoitre")) return Config.domain + "/Images/Icon/tuoitre.jpg";
        
        return Config.domain+"/Images/logo.png";
    }
    public static String getDomainImageMobile(String domain) {
    	
        if (domain.contains("24h")) return Config.domain + "/Images/Icon/24hm.jpg";
        if (domain.contains("cafef")) return Config.domain + "/Images/Icon/cafefm.jpg";
        if (domain.contains("dantri")) return Config.domain + "/Images/Icon/dantrim.jpg";
        if (domain.contains("kenh14")) return Config.domain + "/Images/Icon/kenh14m.jpg";
        if (domain.contains("laodong")) return Config.domain + "/Images/Icon/laodongm.jpg";
        if (domain.contains("ngoisao")) return Config.domain + "/Images/Icon/ngoisaom.jpg";
        if (domain.contains("thethaovanhoa")) return Config.domain + "/Images/Icon/thethaovanhoam.jpg";
        if (domain.contains("vietnamnet")) return Config.domain + "/Images/Icon/vietnamnetm.jpg";
        if (domain.contains("vnexpress")) return Config.domain + "/Images/Icon/vnexpress.jpg";
        if (domain.contains("vtc")) return Config.domain + "/Images/Icon/vtcm.jpg";  
        if (domain.contains("thethao247")) return Config.domain + "/Images/Icon/thethao247m.jpg"; 
        if (domain.contains("thanhnien")) return Config.domain + "/Images/Icon/thanhnienm.jpg";  
        if (domain.contains("vietbao")) return Config.domain + "/Images/Icon/vietbaom.jpg"; 
        if (domain.contains("hanoimoi")) return Config.domain + "/Images/Icon/hanoimoim.jpg"; 
        if (domain.contains("doisongphapluat")) return Config.domain + "/Images/Icon/doisongphapluatm.jpg"; 
        if (domain.contains("danviet")) return Config.domain + "/Images/Icon/danvietm.jpg";
        if (domain.contains("zing")) return Config.domain + "/Images/Icon/zingm.jpg";
        if (domain.contains("afamily")) return Config.domain + "/Images/Icon/afamilym.jpg";
        if (domain.contains("tuoitre")) return Config.domain + "/Images/Icon/tuoitrem.jpg";
       
        return Config.domain+"/Images/logo.png";
    }
    
    public static boolean isNotImage(String url){
    	return (url.toLowerCase().indexOf(".jpg")<=0 && url.toLowerCase().indexOf(".png")<=0 && url.toLowerCase().indexOf(".gif")<=0 && url.toLowerCase().indexOf(".bmp")<=0);
    }
    public static String getWebContent(String urlString) throws IOException{
	      
        try {
           URL url = new URL(urlString);
           //Log.e("fetchJSON", urlString);
           HttpURLConnection conn = (HttpURLConnection) url.openConnection();
           conn.setReadTimeout(10000 /* milliseconds */);
           conn.setConnectTimeout(15000 /* milliseconds */);
           conn.setRequestMethod("GET");
           conn.setDoInput(true);
           //conn.setRequestProperty(field, newValue)
           // Starts the query
           conn.connect();
           //Log.e("fetchJSON", "ok1");
	         //InputStream stream = conn.getInputStream();
           InputStreamReader reader = null;
	         StringBuilder builder = new StringBuilder();

	         try {
	             // ...
	             reader = new InputStreamReader(conn.getInputStream(), "UTF-8");
	             char[] buffer = new char[8192];

	             for (int length = 0; (length = reader.read(buffer)) > 0;) {
	                 builder.append(buffer, 0, length);
	                 
	             }
	         } finally {
	             if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
	         }

	         String html = builder.toString();
	         
	         String data = html;//convertStreamToString(stream);
     		 //data="{\"news\":"+data+"}";
     		 //Log.e("fetchJSON", data);     		 
     		 //stream.close();
     		 return data;
        } catch (Exception e) {
           e.printStackTrace();
           return "0";
        }	         
     		
  }
}